home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 April: Mac OS SDK / Dev.CD Apr 99 SDK1.toast / Development Kits / Open Transport 1.3 / Open Transport SDK / Open Tpt Module Developer / Samples / DLPI Template / StreamLog / CTextDoc.cp < prev    next >
Encoding:
Text File  |  1998-04-30  |  6.1 KB  |  231 lines  |  [TEXT/MPS ]

  1. // ===========================================================================
  2. //    CTextDoc.cp                        ©1994 Metrowerks Inc. All rights reserved.
  3. // ===========================================================================
  4. //
  5. //    A simple text document class. It can handle opening, saving, and
  6. //    reverting TEXT files. It displays the text in a Window and can
  7. //    print the text.
  8.  
  9. #include "CTextDoc.h"
  10. #include "CDirtyText.h"
  11.  
  12. #include <LWindow.h>
  13. #include <LFile.h>
  14. #include <LPrintout.h>
  15. #include <LPlaceHolder.h>
  16. #include <UMemoryMgr.h>
  17. #include <UWindows.h>
  18. #include <String_Utils.h>
  19.  
  20. const ResIDT    WIND_TextDoc        = 200;
  21. const ResIDT    prto_TextDoc        = 201;
  22. const OSType    Creator_DemoDoc        = 'PPd0';
  23. const ResIDT    STRx_Untitled        = 300;
  24.  
  25.  
  26. // ---------------------------------------------------------------------------
  27. //        • CTextDoc(LCommander*, FSSpec*)
  28. // ---------------------------------------------------------------------------
  29. //    Construct a TextDoc associated with the specified file
  30. //
  31. //    If inFileSpec is nil, then create an empty, untitled document
  32.  
  33.  
  34. CTextDoc::CTextDoc(
  35.     LCommander    *inSuper,
  36.     FSSpec        *inFileSpec)
  37.         : LSingleDoc(inSuper)
  38. {
  39.                                     // Create window for our document
  40.     mWindow = LWindow::CreateWindow(WIND_TextDoc, this);
  41.     
  42.                                     // Specify that the text view should
  43.                                     // be the Target when the Window
  44.                                     // is activated
  45.     mTextView = (CDirtyText*) mWindow->FindPaneByID('Text');
  46.     mWindow->SetLatentSub(mTextView);
  47.     
  48.     if (inFileSpec == nil) {
  49.         NameNewDoc();                // Set name of untitled window
  50.         
  51.     } else {
  52.         OpenFile(*inFileSpec);        // Display contents of file in window
  53.     }
  54. }
  55.  
  56.  
  57. // ---------------------------------------------------------------------------
  58. //        • NameNewDoc
  59. // ---------------------------------------------------------------------------
  60. //    Name a new, untitled document window
  61. //
  62. //    Untitled windows start with "untitled", then "untitled 1",
  63. //    "untitled 2", etc. Old numbers are reused, so there won't be
  64. //    gaps in the numbering.
  65. //
  66. //    This routine uses a STR# resource to store the "untitled" string,
  67. //    which can be localized to different languages. The first string
  68. //    is "untitled" and the second is "untitled " (trailing space),
  69. //    which is used when appending a number to the name.
  70.  
  71. void
  72. CTextDoc::NameNewDoc()
  73. {
  74.         // Start with the default name ("untitled")
  75.     Str255    name;
  76.     ::GetIndString(name, STRx_Untitled, 1);
  77.     
  78.     long    num = 0;
  79.     while (UWindows::FindNamedWindow(name) != nil) {
  80.             
  81.             // An existing window has the current name
  82.             // Increment counter and try again
  83.  
  84.         ::GetIndString(name, STRx_Untitled, 2);
  85.         num++;
  86.         Str15    numStr;
  87.         ::NumToString(num, numStr);
  88.         ConcatPStr(name, numStr);
  89.     }        
  90.     
  91.     mWindow->SetDescriptor(name);        // Finally, set window title
  92. }
  93.  
  94.  
  95. // ---------------------------------------------------------------------------
  96. //        • OpenFile
  97. // ---------------------------------------------------------------------------
  98. //    Open a new document for the specified File
  99.  
  100. void
  101. CTextDoc::OpenFile(
  102.     FSSpec    &inFileSpec)
  103. {
  104.         // Create a new File object, read the entire File contents,
  105.         // put the contents into the text view, and set the Window
  106.         // title to the name of the File.
  107.         
  108.     Try_ {
  109.         mFile = new LFile(inFileSpec);
  110.         mFile->OpenDataFork(fsRdWrPerm);
  111.         Handle    textH = mFile->ReadDataFork();
  112.         mTextView->SetTextHandle(textH);
  113.         ::DisposeHandle(textH);
  114.         
  115.         mWindow->SetDescriptor(inFileSpec.name);
  116.         mIsSpecified = true;
  117.     }
  118.     
  119.     Catch_(inErr) {
  120.         delete this;
  121.         Throw_(inErr);
  122.     
  123.     } EndCatch_
  124. }
  125.  
  126.  
  127. // ---------------------------------------------------------------------------
  128. //        • IsModified
  129. // ---------------------------------------------------------------------------
  130. //    Return whether the Document is has changed since the last save
  131.  
  132. Boolean
  133. CTextDoc::IsModified()
  134. {
  135.         // Document has changed if the text view is dirty
  136.     mIsModified = mTextView->IsDirty();
  137.     return mIsModified;
  138. }
  139.  
  140.  
  141. // ---------------------------------------------------------------------------
  142. //        • DoAESave
  143. // ---------------------------------------------------------------------------
  144. //    Save Document in the specified file with the specified file type
  145. //
  146. //    If file type is fileType_Default, use the normal file type for
  147. //    this document
  148.  
  149. void
  150. CTextDoc::DoAESave(
  151.     FSSpec    &inFileSpec,
  152.     OSType    inFileType)
  153. {
  154.     delete mFile;                        // Kill existing file
  155.     
  156.     mFile = new LFile(inFileSpec);        // Make new file object
  157.     
  158.     OSType    fileType = 'TEXT';            // Find proper file type
  159.     if (inFileType != fileType_Default) {
  160.         fileType = inFileType;
  161.     }
  162.                                         // Make new file on disk
  163.     mFile->CreateNewDataFile(Creator_DemoDoc, 'TEXT', 0);
  164.     mFile->OpenDataFork(fsRdWrPerm);
  165.     DoSave();                            // Write out data
  166.                                         // Change window name
  167.     mWindow->SetDescriptor(inFileSpec.name);
  168. }
  169.  
  170.  
  171. // ---------------------------------------------------------------------------
  172. //        • DoSave
  173. // ---------------------------------------------------------------------------
  174. //    Save the entire Document to its associated File (which must already exist)
  175.  
  176. void
  177. CTextDoc::DoSave()
  178. {
  179.                                         // Get text and write to file
  180.     Handle    textH = mTextView->GetTextHandle();
  181.     StHandleLocker    theLock(textH);
  182.     mFile->WriteDataFork(*textH, GetHandleSize(textH));
  183.     
  184.     mTextView->SetDirty(false);            // Saving makes doc un-dirty
  185. }
  186.  
  187.  
  188. // ---------------------------------------------------------------------------
  189. //        • DoRevert
  190. // ---------------------------------------------------------------------------
  191. //    Revert the Document to the last saved version on disk
  192.  
  193. void
  194. CTextDoc::DoRevert()
  195. {
  196.     Handle    textH = mFile->ReadDataFork();
  197.     mTextView->SetTextHandle(textH);
  198.     ::DisposeHandle(textH);
  199.     mTextView->Refresh();
  200. }
  201.  
  202.  
  203. // ---------------------------------------------------------------------------
  204. //        • DoPrint
  205. // ---------------------------------------------------------------------------
  206. //    Print the contents of the Document
  207.  
  208. void
  209. CTextDoc::DoPrint()
  210. {
  211.     LPrintout        *thePrintout = LPrintout::CreatePrintout(prto_TextDoc);
  212.     LPlaceHolder    *textPlace = (LPlaceHolder*)
  213.                                     thePrintout->FindPaneByID('TBox');
  214.     textPlace->InstallOccupant(mTextView, atNone);
  215.     
  216.     thePrintout->DoPrintJob();
  217.     delete thePrintout;
  218. }
  219.  
  220. void
  221. CTextDoc::OutString(char* str, int len)
  222. {
  223.     TEInsert(str, len, mTextView->GetMacTEH());
  224. }
  225.  
  226.  
  227. void
  228. CTextDoc::SetDescriptor(ConstStr255Param name)
  229. {
  230.     mWindow->SetDescriptor(name);
  231. }